//
//  AddGoodsView.m
//  Mall
//
//  Created by 孙飞燕 on 16/10/11.
//  Copyright © 2016年 孙飞燕. All rights reserved.
//

#import "AddGoodsView.h"
#import "AddGoodsTableViewCell.h"
#import "AddGoodsSizeTableViewCell.h"
#import "GoodsModel.h"
@implementation AddGoodsView
{
    NSMutableArray *infoArr;
}
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {

        [self  layOutUI];
        infoArr = [[NSMutableArray alloc]init];
        NSMutableDictionary *infoDic = [[NSMutableDictionary alloc]init];
        [infoDic setValue:[NSNumber numberWithInt:1] forKey:@"goodsNum"];
        GoodsModel *model = [[GoodsModel alloc]initWithDict:infoDic];
        [infoArr addObject:model];

    }
    return self;
}

//在view上添加tableView
- (void)layOutUI{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 50, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame) - 50)];
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.showsVerticalScrollIndicator = NO;
    //注册cell
    [self.tableView registerNib:[UINib nibWithNibName:@"AddGoodsTableViewCell" bundle:nil] forCellReuseIdentifier:@"AddGoodsTableViewCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"AddGoodsSizeTableViewCell" bundle:nil] forCellReuseIdentifier:@"AddGoodsSizeTableViewCell"];

    //添加顶部view
    [self setUpHeadView];
    [self addSubview:self.tableView];
}

#pragma mark --- UITableView的代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        AddGoodsSizeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AddGoodsSizeTableViewCell" forIndexPath:indexPath];
        return cell;

    }else{
       AddGoodsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AddGoodsTableViewCell" forIndexPath:indexPath];
        cell.delegate = self;
       return cell;
    }

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 70;
}

//添加顶部view
- (void)setUpHeadView{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.tableView.frame), 50)];
    view.backgroundColor = [UIColor whiteColor];
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 25, 25)];
    imageView.image = [UIImage imageNamed:@"k0"];

    UILabel *priceLabel = [[UILabel alloc]initWithFrame:CGRectMake(50, 10, 80, 30)];
    priceLabel.text = @"￥1000";
    UILabel *numberLabel = [[UILabel alloc]initWithFrame:CGRectMake(150, 10, 80, 30)];
    numberLabel.text = @"库存250";

    //添加取消按钮view消失
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cancelBtn.frame = CGRectMake(CGRectGetWidth(self.frame) - 80, 10, 50, 30);
    [cancelBtn setImage:[UIImage imageNamed:@"cancel1"] forState:UIControlStateNormal];
    [cancelBtn addTarget:self action:@selector(cancelAction:) forControlEvents:UIControlEventTouchUpInside];

    [view addSubview:imageView];
    [view addSubview:priceLabel];
    [view addSubview:numberLabel];
    [view addSubview:cancelBtn];

    [self addSubview:view];

}
//取消按钮的点击事件
- (void)cancelAction:(UIButton *)btn{
    self.hidden = YES;
}

/**
*  实现加减按钮点击代理事件
*
*  @param cell 当前单元格
*  @param flag 按钮标识，11 为减按钮，12为加按钮
*/
- (void)addGoodsTableViewCellDelegateForGoodsCount:(AddGoodsTableViewCell *)cell andFlag:(int)flag{
//    NSIndexPath *index = [self.tableView indexPathForCell:cell];
//    switch (flag) {
//        case 11:{
//            GoodsModel *model = infoArr[index.row];
//            if (model.goodsNumber > 1) {
//                model.goodsNumber --;
//            }
//            break;
//        }
//        case 12:{
//
//                GoodsModel *model = infoArr[index.row];
//                model.goodsNumber ++;
//
//        }
//                    default:
//            break;
//    }
    int goodsNumber = [(cell.numberTF.text)intValue];
    switch (flag) {
        case 11:
            if (goodsNumber > 1) {
                goodsNumber --;
                cell.numberTF.text = [NSString stringWithFormat:@"%d",goodsNumber];
            }
            break;
        case 12:
            goodsNumber ++;
            cell.numberTF.text = [NSString stringWithFormat:@"%d",goodsNumber];
        default:
            break;
    }
    [self.tableView reloadData];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
